
/*Software serial multple serial test
 
 Receives from the hardware serial, sends to software serial.
 Receives from software serial, sends to hardware serial.
 
 The circuit: 
 * RX is digital pin 2 (connect to TX of other device)
 * TX is digital pin 3 (connect to RX of other device)
 
 created back in the mists of time
 modified 9 Apr 2012
 by Tom Igoe
 based on Mikal Hart's example
 
 This example code is in the public domain.
 */

#include <SoftwareSerial.h>

SoftwareSerial mySerial(2, 3); // RX, TX // Bluetooth Receiving & Bluetooth Transfer

char ledDirection;
int progressValue=0;
int connectionState=0;


const int leftLed=9;
const int leftMiddleLed=10;
const int centerLed=11;
const int rightMiddleLed=12; //numLed
const int rightLed=13;


const int connectionRED = A1;
const int connectionBLUE = A2;
const int connectionGREEN = A0;

const int progressLed0=4;
const int progressLed1=5;
const int progressLed2=6;


const char left = 'q';
const char leftMiddle = 'w';
const char center = 'e';
const char rightMiddle = 'r';
const char right = 't';


const char connectivityLow = 'z';
const char connectivityMedium= 'x';
const char connectivityHigh= 'c';

const char moreProgress = 's';
const char lessProgress = 'a';



void setup()  
{
   // Open serial communications and wait for port to open:
  Serial.begin(9600);

  Serial.println("Goodnight moon!");

  // set the data rate for the SoftwareSerial port
  mySerial.begin(9600);
  mySerial.println("Hello, world?");
  
  
  
  pinMode(leftLed, OUTPUT);
  pinMode(rightLed, OUTPUT);
  pinMode(leftMiddleLed, OUTPUT);
  pinMode(centerLed, OUTPUT);
  pinMode(rightMiddleLed, OUTPUT);
  pinMode(connectionRED, OUTPUT);
  pinMode(connectionBLUE, OUTPUT);
  pinMode(connectionGREEN, OUTPUT);
  pinMode(progressLed0, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  
}


  //Check if the right LED is on otherwise turn on the right LED and shut off all the others


void shutdownLedsExcept(int numLed){
   for(int i=leftLed; i<=rightLed; i++){
     if(i!= numLed){
        digitalWrite(i, LOW);  
        digitalWrite(numLed, HIGH);   
     }
     else{
         digitalWrite(numLed, HIGH);
     }
   }  
}


void BluetoothConnectionStatus(bool connectionStat){
   if(connectionStat == true){ //controlling the ConnectionState
            digitalWrite(connectionRED, HIGH);
            digitalWrite(connectionGREEN, LOW);
            mySerial.write("Status TRUE");
            mySerial.println("-");
    }
    else if(connectionStat = false){
            digitalWrite(connectionGREEN, HIGH);
            digitalWrite(connectionRED, LOW);
            mySerial.write("status FALSE");
            mySerial.println("-");
 }
   
}

void ProgressBar(int progValue){
    if(progValue<= progressValue){
           digitalWrite(progressLed0+progValue,HIGH);
          } else {
           digitalWrite(progressLed0+progValue,LOW); 
          }
  
}

void loop() // run over and over
{
 
   for(int i = 0; i < 5; i++){
      ProgressBar(i);
    }
  
  if (mySerial.available()){
    Serial.write(mySerial.read());
    BluetoothConnectionStatus(true);
  }
  if (Serial.available()){
    mySerial.write(Serial.read()); 
    BluetoothConnectionStatus(false);
  }    
             // DIRECTION SWITCHERS 


    ledDirection = mySerial.read();

       switch(ledDirection){
          case left:
           shutdownLedsExcept(leftLed);
           break;
          case leftMiddle:
           shutdownLedsExcept(leftMiddleLed);
           break;
          case center:
           shutdownLedsExcept(centerLed);
           break;
          case rightMiddle:
           shutdownLedsExcept(rightMiddleLed);
           break;
          case right:
           shutdownLedsExcept(rightLed);
           break;
          case lessProgress:
            progressValue = (progressValue-1)%5;
            break;
          case moreProgress:
            progressValue = (progressValue+1)%5;
            break;
          
       }
    
   

}

     



